//
//  PaymentViewController.m
//  shanHaiHuiApp
//
//  Created by 小和尚 on 2018/3/10.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "PaymentViewController.h"
#import "PaymentTableViewCell.h"
#import "InputMoneyTableViewCell.h"
#import "PayMoneyTableViewCell.h"
#import "PaymentSuccessViewController.h"
#import "PayMoneyWithOutTableViewCell.h"

#import "ChooseNumView.h"


//配送的view
#import "DispatchStyleView.h"

//商品属性
#import "PayMentGoodModel.h"

//商品批号
#import "PayMentSpphModel.h"


#import "PayMentGoodAllTableViewCell.h"






@interface PaymentViewController ()<UITableViewDelegate,UITableViewDataSource>


Strong UITableView * tableView;
Strong NSMutableArray * contentArr;




//头部视图

Strong UIView * headerView;


Strong UIView * lineView;

Strong UIImageView * picImageView;

Strong UILabel * nameLabel;
Strong UILabel * countLabel1;
Strong UILabel * moneyLabel;

//收起

Strong UIView * footView;

Strong CLCustomButton * closeBtn;

Strong CLCustomButton * spreadBtn;

Strong UIButton * closeSoonBtn;

Strong UIButton * spreadSoonBtn;

//修改批次批号
Strong UIButton * changeCardBtn;





//底部购物车视图
Strong UIView * bottomView;
Strong UIImageView * shopImageview;
Strong UILabel * totalPriceLabel;
//销量:
Strong UILabel * numberLabel;
Strong UILabel * countLabel;
Strong UIButton * payBtn;

//保存点了哪一行

Assign NSInteger indexRow;

//会员卡密码
Strong NSString * textStrPass;

//会员卡支付
Strong NSString *memberPay;

//会员卡余额

Strong NSString * memberMoney;


//需支付金额
Strong NSString * needTotalMoney;

//还需支付现金
Strong NSString * needPayMoney;

//还需支付金额
Strong NSString * orderCashAll;

//找零
Strong NSString * orderCashChange;

//选中了哪个一个按钮,跳页面

Strong NSString * index;

//商品批号比较

Strong NSString * strGood;
Strong NSString * strSpph;

// 配送类型
Strong NSString * disttype;

//收货人
Strong NSString * personText;

//联系方式
Strong NSString * contactText;

//配送地址

Strong NSString * dispatchText;

//配送备注

Strong NSString * remarkText;

//门店地址
Strong NSString * shopAdress;

//门店编号
Strong NSString * shopFid;

//做一个字典,保存cell点击了哪一行和他的高度

Strong NSMutableDictionary * indexHeight;

Strong NSString * height;
Strong NSIndexPath * indexPath;

Strong NSDictionary *parameters;

//支付成功后返回的订单编号和token
Strong NSString * orderNo;
Strong NSString * tokenSuccess;

//修改商品批号数组
Strong NSMutableArray * changeSpphArr;

Strong InputMoneyTableViewCell * inputCell;

Strong PayMoneyTableViewCell * cellPayMoney;


Strong NSString * markStr;


Strong NSMutableArray * cardArr;


//标记点击的数组

Strong NSMutableArray * flagArr;

Strong NSMutableArray * sectionArray;


Assign BOOL isOpening;


//标记店面

Strong NSString * storeStr;


//销售金额

Strong NSString * orderXsje;

//销售数量

Strong NSString * orderXssl;

- (void)updateBottomView;


//销售数量数组

Strong NSMutableArray * xsslArr;


Strong NSString * fidStr;


@end

//网络
@implementation PaymentViewController (NetWorking)

//商品批号接口
- (void)getSerialNumDataWith:(NSString *)spph count:(NSString *)count index:(NSInteger )index group:(NSArray *)group{
    [self showprogressHUD];
    
    NSDictionary *parameters = @{
                                 @"spbh":spph
                                 
                                 }.mutableCopy;
    
    [self.changeSpphArr removeAllObjects];
    [[AFAppDotNetAPIClient sharedClient] POST:@"mspphList.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        
  
        
        if ([responseObject[@"code"] integerValue] == 0) {
            
           
            if (self.changeSpphArr.count > 0) {
                
                [self.changeSpphArr removeAllObjects];
            }
            
            for (NSDictionary * dic in responseObject[@"spphList"]) {
                PayMentSpphModel * model = [[PayMentSpphModel alloc] init];
                [model payMentSpphModelWithDic:dic];
                [self.changeSpphArr addObject:model];
            }
      
            ChooseNumView * chooseView = [[ChooseNumView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            [chooseView show];
            chooseView.chooseArr = self.changeSpphArr;
            chooseView.xssl = count;
            chooseView.group = group;
      
          
            
        chooseView.getCompelteClick = ^(NSMutableArray *spphArr) {
                NSMutableArray * arr = [NSMutableArray array];
                
                for (PayMentSpphModel * model in spphArr) {
      
                    NSString * str = [NSString stringWithFormat:@"%@",model.xssl];

                    if ([str isEqualToString:@"0"]) {
     
                    }else{
                        
                        [arr addObject:model];
                    }
                }
   
                
                self.orderSpphArr[index] = arr;
                
            
                
                [self updateCountLabel];
                
                [self updateBottomView];
      
                [self.tableView reloadData];
                
            };
            
           
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

- (void)updateCountLabel{
    NSInteger count = 0;
    CGFloat totalPrice = 0;
    
    for (PayMentGoodModel * model in self.orderArr) {
        count += [model.xssl integerValue];
        CGFloat fdj2 = (CGFloat)[model.xsdj floatValue];
        totalPrice += [model.xssl integerValue] * fdj2;
    }
    
    NSString * totalStr = [NSString stringWithFormat:@"%f",totalPrice];
    NSString * totalStr1 = [self removeFloatAllZero:totalStr];
    
    self.orderXssl = [NSString stringWithFormat:@"%ld",(long)count];
    self.orderXsje = [NSString stringWithFormat:@"%@",totalStr1];
    

    self.needTotalMoney = [NSString stringWithFormat:@"%@",totalStr1];

    
}


//去掉小数点后面的0
-(NSString*)removeFloatAllZero:(NSString*)string
{
    NSString * testNumber = string;
    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
    return outNumber;
}


//提交订单接口
- (void)getMorderPayCheckData{
    [self showprogressHUD];
    
   
    NSDictionary *parameters = @{
                                 @"orgid":[UserModel sharedInstanced].fid,
                                 @"userid":[UserModel sharedInstanced].userID,
                                 @"cardno":self.orderDic[@"fCardno"],
                                 @"cardid":self.orderDic[@"fCardid"],
                                 @"cardpassword":self.textStrPass,
                                 @"orderCardPay":[NSString stringWithFormat:@"%@",self.orderDic[@"fCardMoney"]]
                                 }.mutableCopy;
     NSLog(@"%@",parameters);
    
    [[AFAppDotNetAPIClient sharedClient] POST:@"morderPayCheck.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
       
        if ([responseObject[@"code"] integerValue] == 0) {
            
            self.orderNo = responseObject[@"orderNo"];
           
        }else{
            ShowMessage(responseObject[@"msg"]);
            
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

// 将数组转JSON
- (NSData *)toJSONData:(id)theData{
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] > 0 && error == nil){
        return jsonData;
    }else{
        return nil;
    }
}

//确认支付接口
//默认的时候选第一个
- (void)getConfigSaleDataWithIndex{
    [self showprogressHUD];
    
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:5];
    
    NSString * str = nil;
    for (PayMentGoodModel *model in self.orderArr) {
        NSDictionary *dic = @{@"spbh":model.spbh,
                              @"spmc":model.spmc,
                              @"xsdj":[NSString stringWithFormat:@"%@",model.xsdj],
                              @"xssl":[NSString stringWithFormat:@"%@",model.xssl],
                              @"xsje":[NSString stringWithFormat:@"%f",[model.xsdj floatValue]* [model.xssl floatValue]]
                              };
        [arr addObject:dic];
        
        str = [[NSString alloc] initWithData:[self toJSONData:arr]
                                    encoding:NSUTF8StringEncoding];
    }
    
    NSMutableArray *arrSpph = [NSMutableArray arrayWithCapacity:5];
    
    NSString * strSpphJson = nil;

    for (SpphModel *model in self.orderAllSpphArr) {
        NSDictionary *dict = @{@"fSpbh":model.fSpbh,
                               @"fSite":model.fSite,
                               @"fPh":model.fPh,
                               @"fPc":model.fPc,
                               @"xssl":[NSString stringWithFormat:@"%@",model.xssl]
                               };
        [arrSpph addObject:dict];
        
        strSpphJson = [[NSString alloc] initWithData:[self toJSONData:arrSpph]
                                            encoding:NSUTF8StringEncoding];
    }
    self.parameters = @{
                        @"orgid":[UserModel sharedInstanced].fid,
                        @"userid":[UserModel sharedInstanced].userID,
                        @"cardid":self.orderDic[@"fCardid"],
                        @"cardno":self.orderDic[@"fCardno"],
                        @"cardpassword":self.textStrPass,
                        @"orderXssl":self.orderXssl,
                        @"orderXsje":self.orderXsje,
                        @"orderCardPay":[NSString stringWithFormat:@"%@",self.memberPay],
                        @"orderCashPay":self.needPayMoney,
                        @"orderCashAll":self.orderCashAll,
                        @"orderCashChange":self.orderCashChange,
                        @"token":[UserModel sharedInstanced].token,
                        @"disttype":self.disttype,
                        @"orderSps":str,
                        @"orderSpphs":strSpphJson
                        }.mutableCopy;
    
    NSLog(@"self.parameters === %@",self.parameters);
   
    [[AFAppDotNetAPIClient sharedClient] POST:@"morderPay.do" parameters:self.parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
    
        if ([responseObject[@"code"] intValue] == 0) {
            
      
            [UserModel sharedInstanced].token = responseObject[@"token"];
            
            [UserModel sharedInstanced].orderNo = responseObject[@"orderNo"];
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"是否确认支付"
                                                                           message:nil
                                        preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action) {
               
                                                                      
                                                    }];
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive
                        handler:^(UIAlertAction * action) {
                            
                            PaymentSuccessViewController * vc = [[PaymentSuccessViewController alloc] init];
                            
                            vc.orderNo = responseObject[@"orderNo"];
                            
                            vc.disPatchStyle = self.disttype;
                            
                            [self.navigationController pushViewController:vc animated:YES];
                                                                     
                                                    }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }else{
            
            ShowMessage(responseObject[@"msg"]);
            [UserModel sharedInstanced].token = responseObject[@"token"];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}


//2.开始选择的某个门店的时候调用
- (void)getConfigSaleDataWithIndex:(NSString *)index{
    [self showprogressHUD];

    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:5];
    
    NSString * str = nil;
    for (PayMentGoodModel *model in self.orderArr) {
        NSDictionary *dic = @{@"spbh":model.spbh,
                              @"spmc":model.spmc,
                              @"xsdj":[NSString stringWithFormat:@"%@",model.xsdj],
                              @"xssl":[NSString stringWithFormat:@"%@",model.xssl],
                              @"xsje":[NSString stringWithFormat:@"%@",model.xsje]
                              };
        [arr addObject:dic];
        
        str = [[NSString alloc] initWithData:[self toJSONData:arr]
                                    encoding:NSUTF8StringEncoding];
    }
    
    NSMutableArray *arrSpph = [NSMutableArray arrayWithCapacity:5];
    
    NSString * strSpphJson = nil;
    for (SpphModel *model in self.orderAllSpphArr) {
        NSDictionary *dict = @{@"fSpbh":model.fSpbh,
                              @"fSite":model.fSite,
                              @"fPh":model.fPh,
                              @"fPc":model.fPc,
                              @"xssl":[NSString stringWithFormat:@"%@",model.xssl]
                              };
        [arrSpph addObject:dict];
        
       strSpphJson = [[NSString alloc] initWithData:[self toJSONData:arrSpph]
                                    encoding:NSUTF8StringEncoding];
    
    }
    
    if ([index isEqualToString:@"1"]) {
        NSLog(@"第一个界面");
        self.parameters = @{
                                     @"orgid":[UserModel sharedInstanced].fid,
                                     @"userid":[UserModel sharedInstanced].userID,
                                     @"cardid":self.orderDic[@"fCardid"],
                                     @"cardno":self.orderDic[@"fCardno"],
                                     @"cardpassword":self.textStrPass,
                                     @"orderXssl":self.orderXssl,
                                     @"orderXsje":self.orderXsje,
                                     @"orderCardPay":[NSString stringWithFormat:@"%@",self.memberPay],
                                     @"orderCashPay":self.needPayMoney,
                                     @"orderCashAll":self.orderCashAll,
                                     @"orderCashChange":self.orderCashChange,
                                     @"token":[UserModel sharedInstanced].token,
                                     @"disttype":self.disttype,
                                     @"orderSps":str,
                                     @"orderSpphs":strSpphJson
                                     }.mutableCopy;
    }
    
    
    if ([index isEqualToString:@"2"]) {
        NSLog(@"第er个界面");
        
        NSLog(@"啦啦啦啦啦啦啦啦%@",self.personText);
        
        if (!self.remarkText) {
            
            self.remarkText = @"";
        }
        

        self.parameters = @{
                            @"orgid":[UserModel sharedInstanced].fid,
                            @"userid":[UserModel sharedInstanced].userID,
                            @"cardid":self.orderDic[@"fCardid"],
                            @"cardno":self.orderDic[@"fCardno"],
                            @"cardpassword":self.textStrPass,
                            @"orderXssl":self.orderXssl,
                            @"orderXsje":self.orderXsje,
                            @"orderCardPay":[NSString stringWithFormat:@"%@",self.memberPay],
                            @"orderCashPay":self.needPayMoney,
                            @"orderCashAll":self.orderCashAll,
                            @"orderCashChange":self.orderCashChange,
                            @"token":[UserModel sharedInstanced].token,
                            @"disttype":self.disttype,
                            @"distconsignee":self.personText,
                            @"distconsigneephone":self.contactText,
                            @"distaddress":self.dispatchText,
                            @"distremark":self.remarkText,
                            @"orderSps":str,
                            @"orderSpphs":strSpphJson
                            }.mutableCopy;
    }
    if ([index isEqualToString:@"3"]) {
        
      
        NSLog(@"第san个界面");
        
        if (!self.remarkText) {
            
            self.remarkText = @"";
        }
        
        
        self.parameters = @{
                            @"orgid":[UserModel sharedInstanced].fid,
                            @"userid":[UserModel sharedInstanced].userID,
                            @"cardid":self.orderDic[@"fCardid"],
                            @"cardno":self.orderDic[@"fCardno"],
                            @"cardpassword":self.textStrPass,
                            @"orderXssl":self.orderXssl,
                            @"orderXsje":self.orderXsje,
                            @"orderCardPay":[NSString stringWithFormat:@"%@",self.memberPay],
                            @"orderCashPay":self.needPayMoney,
                            @"orderCashAll":self.orderCashAll,
                            @"orderCashChange":self.orderCashChange,
                            @"token":[UserModel sharedInstanced].token,
                            @"disttype":self.disttype,
                            @"distorgid":self.fidStr,
                            @"distaddress":self.storeStr,
                            @"distremark":self.remarkText,
                            @"orderSps":str,
                            @"orderSpphs":strSpphJson
                            }.mutableCopy;
    }
    if ([index isEqualToString:@"4"]) {
        NSLog(@"第si个界面");
       
        if (!self.remarkText) {
            
            self.remarkText = @"";
        }
        
        self.parameters = @{
                            @"orgid":[UserModel sharedInstanced].fid,
                            @"userid":[UserModel sharedInstanced].userID,
                            @"cardid":self.orderDic[@"fCardid"],
                            @"cardno":self.orderDic[@"fCardno"],
                            @"cardpassword":self.textStrPass,
                            @"orderXssl":self.orderXssl,
                            @"orderXsje":self.orderXsje,
                            @"orderCardPay":[NSString stringWithFormat:@"%@",self.memberPay],
                            @"orderCashPay":self.needPayMoney,
                            @"orderCashAll":self.orderCashAll,
                            @"orderCashChange":self.orderCashChange,
                            @"token":[UserModel sharedInstanced].token,
                            @"disttype":self.disttype,
                            @"distorgid":self.fidStr,
                            @"distaddress":self.storeStr,
                            @"distremark":self.remarkText,
                            @"orderSps":str,
                            @"orderSpphs":strSpphJson
                            }.mutableCopy;
    }
    
    NSLog(@"987654321%@",self.parameters);
    
    [[AFAppDotNetAPIClient sharedClient] POST:@"morderPay.do" parameters:self.parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        
        if ([responseObject[@"code"] intValue] == 0) {
            
            [UserModel sharedInstanced].token = responseObject[@"token"];
          
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"是否确认支付"
                                                                           message:nil
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive
                                                                 handler:^(UIAlertAction * action) {
                                                                     
                                                                     PaymentSuccessViewController * vc = [[PaymentSuccessViewController alloc] init];
                                                                     
                                                                     vc.orderNo = responseObject[@"orderNo"];
                                                                     
                                                                     vc.disPatchStyle = self.disttype;
                                                                     
                                                                     [self.navigationController pushViewController:vc animated:YES];
                                                                     
                                                                 }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
           

        }else{
    
            ShowMessage(responseObject[@"msg"]);
            [UserModel sharedInstanced].token = responseObject[@"token"];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation PaymentViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self NavColor];
    
  
    
}

// 导航颜色
- (void)NavColor{
    
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [UIImage imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64) WihtColor:getColor(navColor) alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    
    
    [bar setShadowImage:[UIImage imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.1) WihtColor:[UIColor blackColor] alphe:1.0]];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"确认订单";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    //http://www.360doc.com/content/15/0125/16/9200790_443596082.shtml
    PayMentGoodModel * model = [[PayMentGoodModel alloc] init];
    model.isOpening = YES;
    
    
    
    //需支付的钱
    self.needTotalMoney = [NSString stringWithFormat:@"%@",self.orderDic[@"orderXsje"]];
    
    //销售金额
    
    NSString * totalStr1 = [self removeFloatAllZero:self.orderDic[@"orderXsje"]];
  
    self.orderXsje = [NSString stringWithFormat:@"%@",totalStr1];
    

    //销售数量
    
    
    self.orderXssl = [NSString stringWithFormat:@"%@",self.orderDic[@"orderXssl"]];
    
    [self updateBottomView];
  
}

- (void)updateBottomView{
    
    
    _totalPriceLabel.text = [NSString stringWithFormat:@"￥%@",self.orderXsje];
    _countLabel.text = [NSString stringWithFormat:@"￥%@",self.orderXssl];
    
}

#pragma Action---


//去掉小数点后面的0
-(NSString*)removeFloatAllZero:(NSString*)string
{
    NSString * testNumber = string;
    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testNumber.floatValue)];
    return outNumber;
}

- (void)payMentClick{
    
    
    if ([self.orderDic[@"fCardMoney"] floatValue] == 0) {
        
        self.textStrPass = @"";
        
    }else{
        
        if (!self.textStrPass) {
            
            ShowMessage(@"请输入支付密码!");
            
            return;
        }
        
        
        
    }
    
   
    
    //余额bu够的情况
    if (([self.orderDic[@"fCardMoney"] integerValue] - [self.orderDic[@"orderXsje"] integerValue]) < 0) {
        
        if (self.orderCashAll.length == 0 || self.orderCashChange.length == 0) {
            
            ShowMessage(@"请输入现金金额");
            
            return;
        }
     
    }
    

 

    [self getMorderPayCheckData];
   
    DispatchStyleView * dispatchView = [[DispatchStyleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [dispatchView show];
    
 
    //价钱和销量
    dispatchView.totalPriceLabel.text = [NSString stringWithFormat:@"￥%@",self.orderXsje];
    dispatchView.countLabel.text = [NSString stringWithFormat:@"￥%@",self.orderXssl];
  
    dispatchView.getMemSelfClick = ^{
        self.disttype = @"mem1";
        self.index = @"1";
    };
    
    ws(dispatchView)
    
    dispatchView.getNameText = ^(DispatchStyleView *view, UITextField *textField) {
    
        self.personText = textField.text;
      
    };
    
    dispatchView.getPhoneText = ^(DispatchStyleView *view, UITextField *textField) {
        
        self.contactText = textField.text;
        
    };
    
    dispatchView.getAdressText = ^(DispatchStyleView *view, UITextField *textField) {
        
        self.dispatchText = textField.text;
        
    };
    
    
    dispatchView.getRemarkViewText = ^(DispatchStyleView *view, UITextView *textView) {
        
        self.remarkText = textView.text;
      
    };
    
    dispatchView.getStoreStr = ^(DispatchStyleView *view, NSString *storeStr,NSString * fidStr) {
        
    
        self.storeStr = storeStr;
        
        self.fidStr = fidStr;
        
    };
    
    
    
    
    dispatchView.getMemDispatch = ^(DispatchStyleView * view){
        self.disttype = @"mem0";
        self.index = @"2";

    };
    
    dispatchView.getStoreClick = ^{
        self.disttype = @"org1";
        self.index = @"3";
      
        
    } ;
    
    dispatchView.getStoreSelf = ^{
        self.disttype = @"org0";
        self.index = @"4";
        
    };
    
    dispatchView.getDispatchClick = ^{
        
     

        if (self.index) {
            
            
            if ([self.index isEqualToString:@"2"]) {
                
                
                if (self.personText.length == 0) {
                    
                    [[UIApplication sharedApplication].keyWindow makeToast:@"请输入收货人" duration:1 position:CSToastPositionCenter];
             
                    return;
                }
                
                if (self.contactText.length == 0) {
                    
                    [[UIApplication sharedApplication].keyWindow makeToast:@"请输入联系方式" duration:1 position:CSToastPositionCenter];
                 
                    return;
                }
                
                if (![TSRegularExpressionUtil validateMobile:self.contactText]) {
                    ShowMessage(@"请输入正确的手机号");
                    return;
                }
                
                if (self.dispatchText.length == 0) {
                    
                    [[UIApplication sharedApplication].keyWindow makeToast:@"请输入配送地址" duration:1 position:CSToastPositionCenter];
           
                    return;
                }
                
                
                NSString * str = self.remarkText;
                
                if ([str isEqualToString:@"如有特殊需求,请填写备注"]) {
                    
                    self.remarkText = @"";
                    
                }else{
                    
                    self.remarkText = self.remarkText;
                    
                }
     
            }
            
            if ([self.index isEqualToString:@"3"]) {
                
                NSString * str = self.remarkText;

                if ([str isEqualToString:@"如有特殊需求,请填写备注"]) {

                    self.remarkText = @"";

                }else{

                    self.remarkText = self.remarkText;

                }
                
                
                
                if (self.storeStr.length == 0) {
                    
                    [[UIApplication sharedApplication].keyWindow makeToast:@"请选择门店" duration:1 position:CSToastPositionCenter];
                    
                    return;
                }
                
                
                
            }
            
            if ([self.index isEqualToString:@"4"]) {
                
                NSString * str = self.remarkText;
                
                if ([str isEqualToString:@"如有特殊需求,请填写备注"]) {
                    
                    self.remarkText = @"";
                    
                }else{
                    
                    self.remarkText = self.remarkText;
                    
                }
                
                
                if (self.storeStr.length == 0) {
                    
                    [[UIApplication sharedApplication].keyWindow makeToast:@"请选择门店" duration:1 position:CSToastPositionCenter];
                    
                    return;
                } 
        
                
            }
            
            
            
             [self getConfigSaleDataWithIndex:self.index];
            
        }else{
           
            self.disttype = @"mem1";
           
            [self getConfigSaleDataWithIndex];
        }
        
        
        [weakdispatchView dismiss];
    };
}


- (void)changeCardBtnClick:(UIButton *)sender{
    
    UIButton *thisBtn = (UIButton*)sender;
    
    NSInteger index = (long)thisBtn.tag;
    
    PayMentGoodModel * goodModel = self.orderArr[index -3];
  
    NSArray * group = self.orderSpphArr[index - 3];

    [self getSerialNumDataWith:goodModel.spbh count:goodModel.xssl index:(index - 3) group:group];
  
}


#pragma init --

- (UITableView *)tableView{
    if (!_tableView) {
        
        if (kDevice_Is_iPhoneX) {
             _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - height64 - UNIT_HEIGHT(49) - height20) style:UITableViewStyleGrouped];
        }else{
            
             _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - height64 - UNIT_HEIGHT(49)) style:UITableViewStyleGrouped];
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        
        
         self.tableView.rowHeight = UITableViewAutomaticDimension;
        
        
        _tableView.bounces = NO;
    }
    return _tableView;
}

// 底部视图

- (UIView *)bottomView{
    if (!_bottomView) {
        
        if (kDevice_Is_iPhoneX) {
             _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT - UNIT_HEIGHT(49) - height20 - height64, SCREEN_WIDTH, UNIT_HEIGHT(49))];
        }else{
           
            _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT - UNIT_HEIGHT(49) - height64, SCREEN_WIDTH, UNIT_HEIGHT(49))];
            
        }

        _bottomView.backgroundColor = [UIColor whiteColor];
        
        _shopImageview = [UIImageView new];
        _shopImageview.contentMode = UIViewContentModeScaleAspectFit;
        _shopImageview.image = [UIImage imageNamed:@"gouwuche"];
        
        _totalPriceLabel = [[UILabel alloc] init];
        _totalPriceLabel.textColor = getColor(@"EC4350");
        _totalPriceLabel.font = DEF_FontSize_18;
       

     
        
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.font = DEF_FontSize_14;
        _numberLabel.textColor = getColor(@"323232");
        _numberLabel.text = @"销量 :";
        
        _countLabel = [[UILabel alloc] init];
        _countLabel.font =DEF_FontSize_14;
        _countLabel.textColor = getColor(@"323232");
  
        
      
        
        _payBtn = [[UIButton alloc] init];
        _payBtn .backgroundColor = getColor(@"EC4350");
        [_payBtn setTitle:@"提交订单" forState:UIControlStateNormal];
        [_payBtn setTitleColor:getColor(@"FFFFFF") forState:UIControlStateNormal];
        [_payBtn addTarget:self action:@selector(payMentClick) forControlEvents:UIControlEventTouchUpInside];
    
        
        [_bottomView sd_addSubviews:@[_shopImageview,_totalPriceLabel,_numberLabel,_countLabel,_payBtn]];
        
        _shopImageview.sd_layout
        .topSpaceToView(_bottomView ,UNIT_HEIGHT(12))
        .leftSpaceToView(_bottomView , UNIT_HEIGHT(16))
        .widthIs(UNIT_WIDTH(30))
        .heightIs(UNIT_HEIGHT(30));
        
        _totalPriceLabel.sd_layout
        .centerYEqualToView(_shopImageview)
        .leftSpaceToView(_shopImageview , UNIT_HEIGHT(6))
        .widthIs(UNIT_WIDTH(100))
        .heightIs(UNIT_HEIGHT(30));
        
        _numberLabel.sd_layout
        .centerYEqualToView(_shopImageview)
        .leftSpaceToView(_totalPriceLabel, UNIT_HEIGHT(6))
        .widthIs(UNIT_WIDTH(50))
        .heightIs(UNIT_HEIGHT(30));
        
        _countLabel.sd_layout
        .centerYEqualToView(_shopImageview)
        .leftSpaceToView(_numberLabel, UNIT_HEIGHT(0))
        .widthIs(UNIT_WIDTH(60))
        .heightIs(UNIT_HEIGHT(30));
        
        [_payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.width.mas_equalTo(UNIT_WIDTH(100));
        }];
    }
    return _bottomView;
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.orderArr.count + 3;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        if ([self.passWordAppear isEqualToString:@"1"]) {
            
            return 4;
            
        }else{
        
            self.textStrPass = @"";
            
            return 3;

        }
    }else if (section == 1){
        
            return 1;
        
    }else if(section == 2){
        
        return 1;
        
    }else{

        NSArray * group = self.orderSpphArr[section - 3];
 
        PayMentGoodModel * goodModel = self.orderArr[section - 3];
        
        if (goodModel.isOpening) {
            
           return group.count;
        }else{
            
            return 0;
        }

    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    for (int i = 3 ; i < self.orderArr.count + 3; i ++) {
        
        if (section == i){
            
            PaymentGoodShowTableViewCell * cell = [PaymentGoodShowTableViewCell cellForTableView:tableView];
            
            PayMentGoodModel * model = self.orderArr[section - 3];
            
            cell.model = model;
            
            cell.getShowClick = ^(PaymentGoodShowTableViewCell *cell, BOOL isOpening) {
                
    
                [self.tableView reloadData];
                
            };
       
            return cell;
  
        }
    }

    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    

 
     for (int i = 3 ; i < self.orderArr.count + 3; i ++) {
         
             if (section == i) {
               
            _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, UNIT_HEIGHT(50))];
                 
            _footView.backgroundColor = getColor(whiteColor);
                 
                 
         _changeCardBtn = [[UIButton alloc] init];
         [_changeCardBtn setTitleColor:getColor(@"EC4350") forState:UIControlStateNormal];
         [_changeCardBtn setBackgroundColor:getColor(whiteColor)];
        _changeCardBtn.tag = section;
         [_changeCardBtn setTitle:@"修改批次批号" forState:UIControlStateNormal];
                 [_changeCardBtn addTarget:self action:@selector(changeCardBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                 
                 _changeCardBtn.layer.cornerRadius = 1.0f;
                 _changeCardBtn.layer.borderWidth = 1.0f;
                 _changeCardBtn.layer.masksToBounds = YES;
                 _changeCardBtn.layer.borderColor = getColor(@"CCCCCC").CGColor;
                 
        
            
            _closeBtn = [[CLCustomButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH  - 55, UNIT_HEIGHT(30), UNIT_HEIGHT(35),UNIT_HEIGHT(35)) type:CLCustomButtonLeftImageType imageSize:CGSizeMake(16,8) midmargin:5];
            _closeBtn.isShowSelectBackgroudColor = NO;
            _closeBtn.imageView.image = [UIImage imageNamed:@"zhankai"];
            _closeBtn.titleLabel.text = @"收起";
            _closeBtn.titleLabel.textColor = getColor(@"CCCCCC");
            _closeBtn.titleLabel.font = DEF_FontSize_11;
            _closeBtn.backgroundColor = [UIColor whiteColor];
            _closeBtn.tag = 100 + section - 3;
                 
                 
            

             PayMentGoodModel * goodModel = self.orderArr[section - 3];
             if (goodModel.isOpening) {
                 [_footView addSubview:self.changeCardBtn];
                 [_footView addSubview:self.closeBtn];
                 
                 [self.changeCardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                     make.top.mas_equalTo(UNIT_HEIGHT(20));
                     make.left.mas_equalTo(UNIT_WIDTH(132));
                     make.width.mas_equalTo(SCREEN_WIDTH - UNIT_WIDTH(132 * 2));
                     make.height.mas_equalTo(UNIT_HEIGHT(30));
                 }];
             }

                 
            // goodModel.isOpening = NO;

            [self.closeBtn touchAction:^(CLCustomButton * _Nonnull button) {

                 goodModel.isOpening = NO;

                 [self.tableView reloadData];

             }];
    
            return _footView;
 
         }
     }
    return nil;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
            if (indexPath.row == 3) {
                
               
                
                InputMoneyTableViewCell * cell = [InputMoneyTableViewCell cellForTableView:tableView];
                
                if ([self.orderDic[@"fCardMoney"] floatValue] == 0) {
                    
                    cell.hidden = YES;
                    
                    
                }else{
                    
      
                    self.inputCell = cell;
        
                    cell.getTextFieldClick = ^(InputMoneyTableViewCell *cell) {
                        self.textStrPass = cell.strPass;
                    };
                    
                    cell.getEyeBtnClick = ^(InputMoneyTableViewCell *cell) {
                        
                        static int i = 0;
                        i ++ ;
                        if (i % 2 == 0) {
                            cell.eyeImageView.image = [UIImage imageNamed:@"yanjing"];
                            NSString *tempPwdStr = cell.passTextField.text;
                            cell.passTextField.text = @"";
                            cell.passTextField.secureTextEntry = NO;
                            cell.passTextField.text = tempPwdStr;
                        }else{
                            cell.eyeImageView.image = [UIImage imageNamed:@"yangjing_guan"];
                            NSString *tempPwdStr = cell.passTextField.text;
                            cell.passTextField.text = @"";
                            cell.passTextField.secureTextEntry = YES;
                            cell.passTextField.text = tempPwdStr;
                        }
                    };
                
                }
                 return cell;
        }
        
        
        
       PaymentGoodTableViewCell * cell = [PaymentGoodTableViewCell cellForTableView:tableView];
        cell.model = self.contentArr[indexPath.row];
        return cell;
    }else if (indexPath.section == 1){
        //根据上面计算会员卡支付金额以及计算还需支付现金以及计算找零
        //余额够的情况
        if (([self.orderDic[@"fCardMoney"] integerValue] - [self.orderDic[@"orderXsje"] integerValue]) > 0) {
            
         PayMoneyWithOutTableViewCell * cell = [PayMoneyWithOutTableViewCell cellForTableView:tableView];
       
            
        cell.payMoneyLabel.text = [NSString stringWithFormat:@"￥%@",self.orderDic[@"orderXsje"]];
    
            cell.memberMoneyLabel.text = [NSString stringWithFormat:@"￥%@",self.orderDic[@"orderXsje"]];
            self.memberPay = self.orderDic[@"orderXsje"];
            
            self.orderCashAll = @"0";
           
            self.orderCashChange = @"0";
            
            self.needPayMoney = @"0";
            
            return cell;
            
        }else{
            //余额不够的情况
            PayMoneyTableViewCell * cell = [PayMoneyTableViewCell cellForTableView:tableView];
            self.cellPayMoney = cell;
            
            cell.payMoneyLabel.text = [NSString stringWithFormat:@"￥%@",self.needTotalMoney];
            
            //会员卡的钱
            cell.memberMoneyLabel.text =  [NSString stringWithFormat:@"￥%@",self.orderDic[@"fCardMoney"]];
         
            float money = [self.needTotalMoney floatValue] - [self.orderDic[@"fCardMoney"] floatValue];
     
            self.memberPay = self.orderDic[@"fCardMoney"];
       
            //现金金额
            
            NSString * moneyStr = [NSString stringWithFormat:@"%f",money];
            
            self.needPayMoney = [self removeFloatAllZero:moneyStr];
            
            cell.needPayLabel.text = [NSString stringWithFormat:@"还需支付现金￥%@",self.needPayMoney];
         
            cell.getTextFieldClick = ^(PayMoneyTableViewCell *cell,UITextField * textField) {
          
                //现金总金额
                NSString * str =  [NSString stringWithFormat:@"%@",textField.text];
                
                NSString * str1 = [self removeFloatAllZero:str];
                
                self.orderCashAll = str1;
                
                float goodCount = 0;
                
                if (str.length  > 0) {
                    
                    goodCount = [textField.text floatValue];
                    
                }
                
                float change = goodCount - money ;
                
                if (change < 0) {
                    
                    ShowMessage(@"输入的金额不正确");
                    
                    change = 0;
                    
                    return ;
                }
                
                NSString * strChange = [NSString stringWithFormat:@"%f",change];
                
                NSString * strChng = [self removeFloatAllZero:strChange];
                
                cell.findMoenyLabel.text = [NSString stringWithFormat:@"找零￥%@",strChng];
                
                self.orderCashChange = strChng;
            };
            return cell;
        
        }
    }else if(indexPath.section == 2){
        
        
        PaymentCardHeaderTableViewCell * cell = [PaymentCardHeaderTableViewCell cellForTableView:tableView];
        
        return cell;
        
    }else{

        PaymentCardTableViewCell * cell = [PaymentCardTableViewCell cellForTableView:tableView];
        
        NSArray * group = self.orderSpphArr[indexPath.section - 3];
        
        cell.model = group[indexPath.row];
   
 
        return cell;
    
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row == 3) {
           
             if ([self.orderDic[@"fCardMoney"] floatValue] == 0) {
                
                 return 0;
                 
            }else{
                
                return 50;
                
            }
     
        }else{


            return 50;
        }
    }else if (indexPath.section == 1){
        if (([self.orderDic[@"fCardMoney"] integerValue] - [self.orderDic[@"orderXsje"] integerValue]) > 0) {
            
            return 100;

        }else{
        
            return 230;
        
        }
    }else if(indexPath.section == 2){
        
        return UNIT_HEIGHT(50);
        
    }else{
 
        return UNIT_HEIGHT(140);
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   
     if (section == 0){
        return 0;
     }else if(section == 1){
         
        return UNIT_HEIGHT(5);
         
     }else if (section == 2){
         
         return UNIT_HEIGHT(5);
         
     }else{
         
        
        return UNIT_HEIGHT(90);

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return UNIT_HEIGHT(5);
    }else if(section == 0){
        return 0;
    }else if (section == 2){
        
        return 0;
        
    }else{
        
        PayMentGoodModel * goodModel = self.orderArr[section - 3];
        
        if (goodModel.isOpening) {
            
           return UNIT_HEIGHT(60);
            
        }else{
            
            return 0;
            
        }
   
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
    
}

#pragma mark --- 懒加载
- (NSMutableArray *)contentArr{
    if (!_contentArr) {
        _contentArr = @[@{@"image":@"huiyuanka拷贝",@"content":[NSString stringWithFormat:@"卡号%@",self.orderDic[@"fCardno"]] ,@"price":self.orderDic[@"fName"] }.mutableCopy,
                      @{@"image":@"yue",@"content":@"会员卡余额",@"price":[NSString stringWithFormat:@"￥%@",self.orderDic[@"fCardMoney"]] }.mutableCopy,
                    @{@"image":@"shouji",@"content":@"手机号",@"price":[NSString stringWithFormat:@"%@",self.orderDic[@"fMobiletel"]] }.mutableCopy,
                        ].mutableCopy;
    }
    return _contentArr;
}



- (NSMutableArray *)changeSpphArr{
    
    if (!_changeSpphArr) {
        
        _changeSpphArr = @[
                           ].mutableCopy;
        
    }
    return _changeSpphArr;
}


- (NSMutableArray *)cardArr{
    
    if (!_cardArr) {
        
        _cardArr = @[].mutableCopy;
    }
    
    return _cardArr;
}

- (NSMutableArray *)xsslArr{
    
    if (!_xsslArr) {
        
        _xsslArr = @[
                     
                     ].mutableCopy;
        
    }
    
    return _xsslArr;
    
}


@end
